import { Component,Injector  } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { BasePage } from '../../../../pages/basePage';
import { StorageService } from '../../../../providers/StorageService';
import { UserService } from '../../../../providers/UserService';
import { PIC_URL} from "../../../../providers/Config";
import { Events } from 'ionic-angular';
/**
 * Generated class for the TicketPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-ticket',
  templateUrl: 'ticket.html',
})
export class TicketPage extends BasePage {
  picrooturl="";//图片服务地址
  userinfo={}//获取内存中的数据

  isOver:boolean;
  ticInfo=null   //订单信息
  data:any;    //获取到的二维码
  data2='';    //获取到的券码
  string=""      //截取后的券码
  constructor(injector: Injector,public events: Events,public userService: UserService,public stc: StorageService) {
    super(injector);
    this.picrooturl=PIC_URL
    this.userinfo=this.stc.read('userinfo'); 
    this.stc.write('back',1);
  }

  ionViewDidLoad() {
    this.ticInfo=this.navParams.get("tic");
    this.getE();
    console.log(this.data2);
    console.log('ionViewDidLoad TicketPage');
    //从待服务传来的
    
    console.log(this.ticInfo)
    if(this.ticInfo==null){
      this.isOver=false;
    }else{
      this.isOver=true;
    }
    
  }
  dismissCLI(){
    this.stc.write("back",this.navParams.get("par"));
    this.viewCtrl.dismiss();
    
  }
  //获取二维码
  async getE(){
    let param={order_sn:this.ticInfo.order_sn}
    let result=await this.userService.setQrCodeAPI(param);
    console.log(result)
    if(result.code == 1){
      this.data=result.data.QRcode;
      this.data2=result.data.payCode;
      console.log(this.data2)
      this.number(this.data2)
    }else{
      let toast = this.toastCtrl.create({
          message: result.msg,
          duration: 1500,
          position: 'bottom',
      })
      toast.present();
    }
  }
  
  //截取券码
  number(i){
    let tem=i.split("");  //把字符串转为数组
    //以4个为循环，如果刚好分完
    if((tem.length%4)==0){
      //总长度除以4，来确定循环几次
      for(let i=0;i<(tem.length/4)+1;i++){
        console.log(1)
        this.string+=tem.slice(0,4).join("")+"   ";
        // this.data2.split("");
       // this.data2.split(y"").splice(0,4);
        tem.splice(0,4);
        console.log(this.string)
      }
    }else{
      //如果有多出来的
      console.log(2)
      for(let i=0;i<Math.ceil(tem.length/4);i++){
          if(tem.length>4){
            this.string+=tem.slice(0,4).join("")+"   ";
            tem.splice(0,4);
            console.log(tem)
          }
          if(tem.length<4){
            this.string+=tem.join("");
            tem=[];
          }
      }
    }
  }
 
}
